<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('menu_type_form.class.php');
require_once('menu_type_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_type_dbc.class.php');


class Menu_Type_Form_Ctrl extends GForm_Ctrl_With_Object {
	private $htdocs_path;
	private $hidden_inc_path;

	function __construct($system_name, Vars_Manager $vm, DB_Connection $dbc, $object_id_capture = 'obj_id', $htdocs_path, $hidden_inc_path) {
		$this->htdocs_path = tangra_normalize_path($htdocs_path);
		$this->hidden_inc_path = tangra_normalize_path($hidden_inc_path);

		parent::__construct($system_name, $vm, $dbc, $object_id_capture);

		$this->export('htdocs_path', $this->htdocs_path);
		$this->export('hidden_inc_path', $this->hidden_inc_path);
	}


	protected function create_form() {
		$form = new Menu_Type_Form();

//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new Menu_Type_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		if (!file_exists($this->hidden_inc_path.$this->form->get_field_value('class_path'))) {
			$this->form->set_field_error('class_path', 'file_not_exist');
			$this->export('class_path', $this->form->get_field_value('class_path'));
			$ret = false;
		}

		if (!ereg("[a-zA-Z0-9_]{1,255}", $this->form->get_field_value('class_name'))) {
			$this->form->set_field_error('class_name', 'invalid_class_name');
			$this->export('class_name', $this->form->get_field_value('class_name'));
			$ret = false;
		}

		if (!file_exists($this->htdocs_path.$this->form->get_field_value('page_manage_path'))) {
			$this->form->set_field_error('page_manage_path', 'file_not_exist');
			$this->export('page_manage_path', $this->form->get_field_value('page_manage_path'));
			$ret = false;
		}

		return $ret;
	}


	protected function on_bad_submit() {

//		printbr('lajna');
	}


	protected function create_object() {
		$object = new TCMS_menu_type_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('name', $object->get_name());
		$form->set_field_value('class_path', $object->get_class_path());
		$form->set_field_value('class_name', $object->get_class_name());
		$form->set_field_value('page_manage_path', $object->get_page_manage_path());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_name($form->get_field_value('name'));
		$object->set_class_path($form->get_field_value('class_path'));
		$object->set_class_name($form->get_field_value('class_name'));
		$object->set_page_manage_path($form->get_field_value('page_manage_path'));
	}
}